package com.baizhi.jishi.service.impl;

import com.baizhi.jishi.dao.DepartmentDao;
import com.baizhi.jishi.entity.Department;
import com.baizhi.jishi.entity.MyTree;
import com.baizhi.jishi.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao departmentDao;
    @Override
    public Map showPage(int page, int limit, String name) {
        if (name != null && !name.trim().equals("")){
            name = "%"+name+"%";
        }else {
            name = null;
        }
        //System.out.println(name);
        page = page-1;
        List<Department> departments = departmentDao.showPage(page, limit, name);
        int count = departmentDao.getCount(name);
        Map map = new HashMap();
        map.put("count",count);
        map.put("data",departments);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    @Override
    public Map adddepartment(Department department) {
        Map map = new HashMap();
        int i = departmentDao.adddepartment(department);
        if (i>0){
            map.put("addResult",true);
        }else {
            map.put("addResult",false);
        }
        return map;
    }

    @Override
    public Map deleteDepartment(int id) {
        Map map = new HashMap();
        int i = departmentDao.deleteDepartment(id);
        if (i>0){
            map.put("deletResult",true);
        }else {
            map.put("deletResult",false);
        }
        return map;
    }

    @Override
    public Map selectOne(int id) {
        Department department = departmentDao.selectOne(id);
        Map map = new HashMap();
        map.put("department",department);
        return map;
    }

    @Override
    public Map updateDepartment(Department department) {
        Map map = new HashMap();
        int i = departmentDao.updateDepartment(department);
        if (i>0){
            map.put("updateResult",true);
        }else {
            map.put("updateResult",false);
        }
        return map;
    }

    @Override
    public Map deleteSome(int[] ids) {
        Map map = new HashMap();
        int i = departmentDao.deleteSome(ids);
        if (i>0){
            map.put("deleteResult","删除成功");
        }else {
            map.put("deleteResult","删除失败");
        }
        return map;
    }

    @Override
    public List<MyTree> showAll() {
        return departmentDao.showAll();
    }

}
